import Vue from 'vue'
import VueRouter from 'vue-router'
import customRoutes from './module/custom'
import store from "@/store";
import {Message} from "element-ui";

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    meta: {
      title: '主功能 - 中国移动研究院云展工具',
      auth: true
    },
    component: () => import('@/views/Home.vue')
  },
  {
    path: "/login",
    name: 'login',
    meta: {
      title: '欢迎来到 - 中国移动研究院云展工具'
    },
    component: () => import('@/views/Login.vue')
  },
  {
    path: "/exhibition",
    name: 'exhibition',
    meta: {
      title: '展会规划 - 中国移动研究院云展工具',
      auth: true
    },
    component: () => import('@/views/Exhibition.vue')
  },
  {
    path: "/expomodule",
    name: 'expomodule',
    meta: {
      title: '中国移动研究院云展工具',
      auth: true
    },
    component: () => import('@/views/ExpoModule.vue')
  },
  {
    path: "/firstExpomodule",
    name: 'firstExpomodule',
    meta: {
      title: '中国移动研究院云展工具',
      auth: true
    },
    component: () => import('@/views/FirstExpoModule.vue')
  },
  {
    path: '/about',
    name: 'about',
    meta: {
      title: 'About',
      noFeature: true
    },
    component: () => import('@/views/About.vue')
  },
  {
    path: '/information',
    name: 'information',
    meta: {
      title: 'Information',
      noFeature: true
    },
    component: () => import('@/views/Information.vue')
  },
  {
    path: '/support',
    name: 'support',
    meta: {
      title: 'Support',
      noFeature: true
    },
    component: () => import('@/views/Support.vue')
  },
  {
    path: '/contact',
    name: 'contact',
    meta: {
      title: 'Contact',
      noFeature: true
    },
    component: () => import('@/views/Contact.vue')
  },
  ...customRoutes
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  window.scrollTo(0, 0)
  if (to.meta.keywords) {
    document.querySelector('meta[name="keywords"]').setAttribute('content', to.meta.keywords)
  }
  if (to.meta.description) {
    document.querySelector('meta[name="description"]').setAttribute('content', to.meta.description)
  }
  if (to.meta.noFeature) {
    Message.info('更多内容正在建设，敬请期待！')
    next({
      path: '/login',
    })
    return
  }
  if (to.meta.title) {
    document.title = `${to.meta.title}`
  }
  // 判断是否需要登录
  const isNotNeedAuth = to && to.query && to.query.isShare == "1"
  console.log(to.query,isNotNeedAuth)
  if (isNotNeedAuth) {
    next()
    return
  }
  if (to.meta.auth && !store.getters["user/token"]) {
    next({
      path: '/login',
      query: {redirect: to.fullPath}
    })
  }
  next()
})

export default router
